package de.hpi.generator.config;


import java.util.ArrayList;

/**
 * configuration table model
 * @author Michael Wolowyk
 */
public class TableModel {


    public String name;
        public int rowNumbers = 0;
        public ArrayList<ColumnModel> columns = new ArrayList<ColumnModel>();
        public DependencyModels FunctionalDependencies = null;
	
        public TableModel(){
		
	}
	
        /**
         * 
         * @param aName a table name
         * @param aRowNumbers min number of rows in the table
         * note: because of inclusion dependencies the number of rows isn't garanted
         */
        public TableModel(String aName, int aRowNumbers) {
		this.name  = aName;
		this.rowNumbers  = aRowNumbers;
	}
	


        /**
         * adds a column model to columns
         * @param aColumnModel
         * @throws ExistsException if a column model already exists in collection
         */
        public void addColumnModel(ColumnModel aColumnModel) throws ExistsException {
            // if column not exists in the collection yet
		if (findColumnModelByName(aColumnModel.name) == null) {
			this.columns.add(aColumnModel);
		} else {
			throw new ExistsException("The table " + aColumnModel.name + " is already exists");
		}
	}

	
	
	@Override
	public String toString(){
		String result =  String.format("{name: %s, rowNumbers: %d \n \tcolumns:[ \n", this.name, this.rowNumbers);
		String aColumnModelsString = "";
		for (ColumnModel aColumnModel : columns) {
			aColumnModelsString+="\t\t" + aColumnModel.toString();
		}
		return result + aColumnModelsString + "\t]}\n";
	}


        /**
         * finds Column Model By Name
         * @param aColumnModelName
         * @return a founded Column Model
         */
        public ColumnModel findColumnModelByName(String aColumnModelName) {
		ColumnModel result = null;
		for (ColumnModel aTestObject : this.columns) {
			if(aTestObject.name.compareTo(aColumnModelName) == 0) {
				 result = aTestObject;
			}
		}
		return result;
	}

}
